package com.shanghaionstar.servicepackage.model;

import android.content.Context;

import com.onstar.cn.api.GprsPackageService;
import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.common.util.Constants;

import com.onstar.cn.framework.annotation.http.HeadsValue;
import com.onstar.cn.framework.annotation.http.Request;
import com.onstar.cn.framework.mvp.base.core.BaseService;
import com.onstar.cn.framework.network.HttpInjectUtils;
import com.onstar.cn.framework.network.entity.RequestEntity;
import com.onstar.cn.framework.network.enumtype.HttpAccept;
import com.onstar.cn.framework.network.enumtype.HttpMethod;
import com.onstar.cn.framework.network.enumtype.Language;
import com.onstar.cn.framework.utils.GsonHelper;
import com.onstar.cn.payme.response.GetPackageListResponse;
import com.shanghaionstar.utils.ApplicationUtil;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.NetConnectUtil;

import rx.Observable;
import rx.functions.Func0;

/**
 * 获取安吉星套餐列表
 * Created by Yonglang Hong on 2016/8/8.
 */
public class OnstarPkgModel extends BaseService{

    private Context context;

    public OnstarPkgModel(Context context) {
        this.context = context;
    }

    @Override
    public RequestEntity getRequestEntity() {
        return null;
    }

    @Override
    public Object jsonToBean(String json) {
        return null;
    }

    public Observable<GetPackageListResponse> loadPackageList() {
        return Observable.defer(new Func0<Observable<GetPackageListResponse>>() {
            @Override
            public Observable<GetPackageListResponse> call() {
                try {
                    return Observable.just(doLoadPackageList());
                } catch (OnStarException e) {
                    return Observable.error(e);
                }
            }
        });
    }


    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.GET)
    public GetPackageListResponse doLoadPackageList() throws OnStarException {
        String subscriberID = ApplicationUtil.getInstance().getSubscriberID();
        String accountID = ApplicationUtil.getInstance().getAccountId();
        String vin = ApplicationUtil.getInstance().getVin();

        GprsPackageService service = new GprsPackageService(Constants.CURRENT_SERVER_URL,context);
        RequestEntity requestEntity = NetConnectUtil.getAuthRequestEntity(context);
        service.getGprsPackageService(requestEntity, subscriberID, accountID, vin);
        requestEntity = HttpInjectUtils.injectHttp(getClass(), "doLoadPackageList", requestEntity);
        String response = NetConnectUtil.getResponse(getDatas(requestEntity));
        GetPackageListResponse listResponse = GsonHelper.fromJson(response,GetPackageListResponse.class);
        return listResponse;
    }
}
